--[[---
  server/init.lua
  @depends: config.lua
  @requires: socket
--]]------
print('server/init.lua: initializing:')
assert(config) -- test dependencies

-- coroutine-able (x)pcall
-- require 'coxpcall'
dofile '/libs_l/coxpcall.lua'

-- load luasocket namespace
io.write "\tLoading sockets library...\t"
socket = require("socket") --place to hold the socket library
print (socket._VERSION, '\t[OK!]')

-- load copas namespace
io.write "\tLoading copas library...\t"
copas = require("copas") --place to hold the copas library
print (copas._VERSION, '\t[OK!]')

-- table to map sockets to clients
sockets = {}

-- assorted utilities
utils = require "utils" --place to hold different utilities
-- encode different CP's
encode = require "encode"
-- communications routines (read/write from sockets)
-- dofile "/server/comms.lua"
dofile "/server/newcomms.lua"
-- stuff to handle if client is in menu
dofile "/server/menuhandlers.lua"
-- stuff to handle new clients connecting
dofile "/server/logon.lua"
-- account handling
dofile "/server/account.lua"
-- commands handlers
dofile "/server/commands.lua"

  -- mxp support
  -- dofile "mxp.lua"
  
  --some telnet definitions
  -- dofile "telnet.lua"

-- build handlernames table:
for k,v in pairs(handlers) do
 handlernames[v] = k
end
  
-- create a TCP socket and bind it to the local host
io.write "\tBinding server port...\t"
tcpserver = assert (socket.bind ("*", config.port))
copas.addserver(tcpserver, MainHandler)

print ("Ready, on port", config.port,'\t[OK!]')
-- end -- Init
print('Initialization complete.')
